/***************************************************************************//**
 * @file MSc_Exception.h
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2013 Marek M. Cel
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/
#ifndef MSC_EXCEPTION_H
#define MSC_EXCEPTION_H

////////////////////////////////////////////////////////////////////////////////

#include <string>

////////////////////////////////////////////////////////////////////////////////

/** Exception handling class. */
class MSc_Exception
{
public:

    /** Exceptions types. */
    enum Type
    {
        UnknownException    = 0,    ///< undefined exception
        ArrayIndexOverLimit = 1,    ///< wrong value of array index
        NullPointer         = 2,    ///< unexpected null pointer
        UnexpectedNaN       = 3,    ///< unexpected Not-a-Number value
        FileNotFound        = 4     ///< file does not exist
    };

    /** Constructor. */
    MSc_Exception() :
        m_cause( 0 )
    {
        m_type = UnknownException;
        m_line = 0;
        m_file = "Unknown file";
        m_info = "Unknown exception.";
    }

    /** Copy constructor. */
    MSc_Exception( const MSc_Exception &e )
    {
        m_cause = e.m_cause;

        m_type = e.m_type;
        m_line = e.m_line;
        m_file = e.m_file;
        m_info = e.m_info;

    }

    /** Destructor. */
    virtual ~MSc_Exception()
    {
        if ( m_cause ) delete m_cause; m_cause = 0;
    }

    /** */
    inline bool hasCause() const
    {
        return ( m_cause ) ? true : false;
    }

    /** */
    inline const MSc_Exception& getCause() const
    {
        return (*m_cause);
    }

    /** */
    inline Type getType() const
    {
        return m_type;
    }

    inline int getLine() const
    {
        return m_line;
    }

    /** */
    inline std::string getFile() const
    {
        return m_file;
    }

    /** */
    inline std::string getInfo() const
    {
        return m_info;
    }

    /** */
    inline void removeCause()
    {
        if ( m_cause ) delete m_cause; m_cause = 0;
    }

    /** */
    inline void setCause( const MSc_Exception &exception )
    {
        removeCause();

        m_cause = new MSc_Exception( *(exception.m_cause) );
    }

    /** */
    inline void setType( Type type )
    {
        m_type = type;
    }

    /** */
    inline void setLine( int line )
    {
        m_line = line;
    }

    /** */
    inline void setFile( std::string file )
    {
        m_file = file;
    }

    /** */
    inline void setFile( const char *file )
    {
        m_file = file;
    }

    /** */
    inline void setInfo( std::string info )
    {
        m_info = info;
    }

    /** */
    inline void setInfo( const char *info )
    {
        m_info = info;
    }

private:

    MSc_Exception *m_cause;     ///< exception cause (if exception triggered by catching other exception)
    Type m_type;                ///< exception type
    int m_line;                 ///< line number
    std::string m_file;         ///< file name
    std::string m_info;         ///< extra informations

};

////////////////////////////////////////////////////////////////////////////////

#endif // MSC_EXCEPTION_H
